﻿using System;
using System.Reflection;

using Xin478.CSWork.Common;
using Xin478.MySocket;

namespace Xin478.CSWork.Server
{
    public abstract class ResponseBase : IResponse
    {
        protected IServer correspondent;
        public void StartFilter(IServer correspondent)
        {
            this.correspondent = correspondent;
            this.correspondent.CommandReceived += new Xin478.CSWork.Common.CommandReceiveHandler(correspondent_CommandReceived);
        }

        void correspondent_CommandReceived(DatagramBuilder builder, ref bool cancel)
        {
            MethodInfo mi = this.GetType().GetMethod(builder.Name, BindingFlags.NonPublic | BindingFlags.IgnoreCase | BindingFlags.Instance);
            if (mi != null)
            {
                mi.Invoke(this, builder.Params);
                cancel = true;
            }
        }
    }
}
